<?php
/**
 * @version	$Id: image.php $
 * @package	F5Gallery
 * @subpackage	Component
 * @copyright	Copyright (C) 2010 YOS.,JSC. All rights reserved.
 * @license	Commercial
 */
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
class actionDrupalpost extends JObject {
	
	var $_dbo	=	null;
	var $data_tree	=	array();
	
	function __construct()
	{
		global $mainframe;
		parent::__construct();
		$this->getDBO();
	}	
	function action()
	{
		$action	=	JRequest::getVar('action','changestyle');
		$data	=	$this->$action();
		return $data;
	}
	function changestyle()	
	{
		$style	=	JRequest::getVar('style','story');
		$db		=	$this->_dbo;
		$query	=	"SELECT  V.*
						FROM `#__vocabulary` as V, `#__vocabulary_node_types` as VNT
						WHERE V.vid=VNT.vid 
							AND VNT.type=".$db->Quote($style);
		$db->setQuery($query);
		$options	=	$db->loadObjectList();		
		
		$html	=	array();
		if (count($options)) {
			
			for ($i=0;$i<count($options);$i++)
			{
					$query	=	"SELECT  TD.tid,TD.name 
						FROM `#__term_data` as TD, `#__vocabulary` as V
						WHERE TD.vid = V.vid
							AND V.vid=".$options[$i]->vid;
					
					$db->setQuery($query);				
					$option	=	$db->loadObjectList();					
					
					$query	=	"SELECT  TH.*
					FROM `#__term_hierarchy` as TH,`#__term_data` as TD
							where TH.tid=TD.tid and TD.vid=".$options[$i]->vid;
					$db->setQuery($query);
					
					$term_hierarchy	=	$db->loadObjectList();
					$this->data_tree	=	array();
					$this->sort($option,$options[$i]->vid,$term_hierarchy,0,0);
					
					$option	=	array();
					for ($j=0;$j<count($this->data_tree);$j++)
					{
						$index	=	$this->data_tree[$j]->index;
						$opt	=	$this->data_tree[$j]->data;
						$str	=	"";
						for ($k	=0;$k<intval($index);$k++)
						{
							$str	=	$str.'-';
						}
						$opt->name	=	$str.$opt->name;
						$option[]=$opt;
					}
					
					$voca	=	array();
					
					$voca['title']		=	$options[$i]->name;
					$voca['required']	=	$options[$i]->required;
					$voca['multiple']	=	$options[$i]->multiple;
					$voca['tags']		=	$options[$i]->tags;
					
					$text	=	"";
					$multi	=	"";
					if ($options[$i]->required) {
						$text	=	'required';
					}
					if ($options[$i]->multiple || $options[$i]->tags) {
						if (count($option)<10) {
							$multi	=	' multiple size="'.count($option).'"';
						}
						else {
							$multi	=	' multiple size="10"';
						}
					}				
					array_unshift($option, JHTML::_('select.option', '0', '- '.JText::_('select').' -', 'tid', 'name'));
					$voca['html']		=	JHTML::_('select.genericlist', $option, 'a_'.$i.'[]', $multi.' class="termdata '.$text.'" rel="'.$voca['title'].'"', 'tid', 'name', '');
					$html[$i]			=	$voca;
			}
			ob_start()	;
				?>
				<div class="voca">
					<?php
					for ($j=0;$j<count($html);$j++)
					{
						$voca	=	$html[$j];
					
					?>
						<div class="cols">
							<div class="title"><?=  $voca['title']; ?><?php if($voca['required']) echo '<span style="color:red; font-weight:bold; margin:0px 0 0px 3px;">'. JText::_('(*)').'</span>'; ?></div>
							<div class="body"><?php echo $voca['html']; ?></div>
						</div>
					<?php
					}
				?>
				<div class="clr"></div>
				<div style="color:red; font-weight:bold; margin:8px 0 3px 0;">(*): is required</div>
				</div>
				<?php
			$str	=	ob_get_contents();
			ob_end_clean();
			return $str;
		}
		else {
			return JText::_('please select note type other');
		}
	}
	
	function sort($term,$vid,$term_hierarchy,$k,$index)
	{
		$tid_arr	=$this->getArrayhierarchy($term_hierarchy,$k);
		
		if (count($tid_arr)==0) {
			return false;
		}		
		for ($j=0;$j<count($tid_arr);$j++)
		{
			$rowdata	=	new stdClass();
			$rowdata->index	=	$index;
			$data	=	 $this->getAterm($term,$tid_arr[$j]);			
			$rowdata->data	=	$data;
			array_push($this->data_tree,$rowdata);
			$index1	=	$index+1;
				$this->sort($term,$vid,$term_hierarchy,$tid_arr[$j],$index1);
		}		
	}
	function getAterm($term,$tid)
	{
		for ($i=0;$i<count($term);$i++)
		{
			if ($term[$i]->tid==$tid) {			
				return $term[$i];
			}
		}
	}
	function getArrayhierarchy($term_hierarchy,$value)
	{
		$arr	=	array();
		for($i=0;$i<count($term_hierarchy);$i++)
		{
			if ($term_hierarchy[$i]->parent==$value) {
				$arr[]	=	$term_hierarchy[$i]->tid;
			}
		}		
		return $arr;
	}
	
	function getDBO()
	{
		require(JPATH_COMPONENT_ADMINISTRATOR.DS.'cms'.DS.'drupalpost'.DS.'cms_config.php');		
		$option_ 	= array(); //prevent problems 
		$option_['driver']   = $d_w_driver;       	// Database driver name
		$option_['host']     = $d_w_host;    		// Database host name
		$option_['user']     = $d_w_user;  	    	// User for database authentication
		$option_['password'] = $d_w_password;  		// Password for database authentication
		$option_['database'] = $d_w_database;  	  	// Database name
		$option_['prefix']   = $d_w_prefix;          // Database prefix (may be empty)
		if (empty($option_['user']) || empty($option_['password']) || empty($option_['database']) || empty($option_['driver']) || empty($option_['host']))
		{
			$config	=	JFactory::getConfig();
			
			$option_['driver']   = $config->getValue('dbtype');       	// Database driver name
			$option_['host']     = $config->getValue('host');    		// Database host name
			$option_['user']     = $config->getValue('user');  	    	// User for database authentication
			$option_['password'] = $config->getValue('password');  		// Password for database authentication
			$option_['database'] = $config->getValue('db');  	  	// Database name
		}
		$db_	= &JDatabase::getInstance( $option_ );
		if (!empty($db_->message)) {
				$this->msg = JText::sprintf($db_->message);
				return false;
			}	
		$this->_dbo=$db_;
		return $this->_dbo;
	}
}
